package com.exsun.gateway.jt809.start;

import com.exsun.gateway.jt809.config.NettyClientConfig;
import com.exsun.gateway.jt809.handler.cache.AuthorizeCode;
import com.exsun.gateway.jt809.handler.SendMgrHandler;
import exsun.jt808.data.jt809_data.video.main.UpAuthorizeMsgStartup;
import exsun.jt808.data.util.executorsThread.ScheduledHandle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 定时任务
 */
@Component
public class TimerStaskListener implements ApplicationListener<ApplicationReadyEvent> {

	private final static Logger logger = LoggerFactory.getLogger(TimerStaskListener.class);

    @Autowired
    private SendMgrHandler sendMgrHandler;

	@Autowired
	private NettyClientConfig nettyClientConfig;

	@Override
	public void onApplicationEvent(final ApplicationReadyEvent event) {
        updateAgingPassword();
	}

    /**
     * 定时更新视频播发的时效口令
     */
    public void updateAgingPassword(){
        Runnable runnable = new Runnable() {
            public void run() {
                try {
					UpAuthorizeMsgStartup data = new UpAuthorizeMsgStartup();
					data.setPlantId(nettyClientConfig.getPlatformId());
					String code = AuthorizeCode.resetAuthorizeCode();
					data.setBelongAreaAuthorizeCode(code);
					data.setTransAreaAuthorizeCode(code);
					sendMgrHandler.agingPassword(data);
                }catch (Exception e){
                    logger.error(e.getMessage());
                }
            }
        };
        // 第二个参数为首次执行的延时时间，第三个参数为定时执行的间隔时间
		ScheduledHandle.getInst().getService().scheduleAtFixedRate(runnable, 0, 1, TimeUnit.MINUTES);
    }



}

